/**
 * @description 删除模板
 */
const inquirer = require('inquirer')
const fs = require('fs')
const chalk = require('chalk')
const { templateList } = require('../../data/template')
const removeQuestion = [
    {
        name: 'name',
        type: 'input',
        message: '请输入要删除的模板名称',
        validate(val) {
            if (!val) {
                return '名称为必填项'
            } else if (!templateList.some(item => item.name === val)) {
                return '名称不存在'
            } else {
                return true
            }
        }
    }
]

module.exports = () => {
    console.clear()
    inquirer.prompt(removeQuestion).then(answer => {
        let index = templateList.findIndex(item => item.name === answer.name)

        // 删除模板信息
        templateList.splice(index, 1)
        // 重写文件
        let data = 'exports.templateList = '+JSON.stringify(templateList)
        fs.writeFile(process.cwd() + '/lib/data/template.js', data, err => {
            if (err) {
                console.log(chalk.redBright('删除失败'))
                console.log(err)
            }
            console.log(chalk.greenBright('删除成功'))
            console.table(templateList)
        })
    })
}
